package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.edie.customview.R;

import java.util.ArrayList;

public class ColorActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "ColorActivity";
    private Button discernBtn;
    private ImageView imgIv;


    private RecyclerView colorRv;

    public static void start(Context context) {
        context.startActivity(new Intent(context, ColorActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        initView();
        colorRv.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initView() {
        discernBtn = (Button) findViewById(R.id.discern_btn);
        imgIv = (ImageView) findViewById(R.id.img_iv);

        discernBtn.setOnClickListener(this);


        colorRv = (RecyclerView) findViewById(R.id.color_rv);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.discern_btn:
//                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_img);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ff0_2);
                imgIv.setImageBitmap(bitmap);
                Palette palette = Palette.from(bitmap).generate();
                Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();

                Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
                Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的，暗色
                Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的，亮色
                Palette.Swatch muted = palette.getMutedSwatch();//柔和的
                Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的，暗色
                Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色

                ArrayList<Palette.Swatch> swatches = new ArrayList<>();
                swatches.add(vibrant);
                swatches.add(vibrantDark);
                swatches.add(vibrantLight);
                swatches.add(muted);
                swatches.add(mutedDark);
                swatches.add(mutedLight);
                colorRv.setAdapter(new A(swatches));
                break;
        }
    }


    static class A extends RecyclerView.Adapter<A.AViewHolder> {
        ArrayList<Palette.Swatch> mData;

        public A(ArrayList<Palette.Swatch> data) {
            mData = data;
        }

        @NonNull
        @Override
        public AViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_color, parent, false);
            return new AViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull AViewHolder holder, int position) {
            Palette.Swatch vibrant = mData.get(position);
            if (vibrant != null) {
                //
                int rgb = vibrant.getRgb();
                holder.rgbIv.setBackgroundColor(rgb);

                //
                float[] hsl = vibrant.getHsl();
//                    hslIv.setBackgroundColor(vibrant.getHsl());
                Log.i(TAG, "onClick: " + hsl);

                //
                int population = vibrant.getPopulation();
                holder.countTv.setText("数量" + population);

                //
                int bodyTextColor = vibrant.getBodyTextColor();
                holder.zhuTv.setBackgroundColor(bodyTextColor);
                //
                int titleTextColor = vibrant.getTitleTextColor();
                holder.titleTv.setBackgroundColor(titleTextColor);
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        static class AViewHolder extends RecyclerView.ViewHolder {
            ImageView rgbIv;
            ImageView hslIv;
            TextView countTv;
            TextView zhuTv;
            TextView titleTv;

            public AViewHolder(View itemView) {
                super(itemView);
                rgbIv = (ImageView) itemView.findViewById(R.id.rgb_iv);
                hslIv = (ImageView) itemView.findViewById(R.id.hsl_iv);
                countTv = (TextView) itemView.findViewById(R.id.count_tv);
                zhuTv = (TextView) itemView.findViewById(R.id.zhu_tv);
                titleTv = (TextView) itemView.findViewById(R.id.title_tv);
            }
        }
    }
}
